package com.jm.springboot21.controller;


import com.jm.springboot21.pojo.Dept;
import com.jm.springboot21.pojo.PageBean;
import com.jm.springboot21.pojo.Result;
import com.jm.springboot21.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dept")
@CrossOrigin("*")
public class DeptController {
    @Autowired
    private DeptService deptService;
    @GetMapping
    public Result findAll(){
        List<Dept> all = deptService.findAll();
        return Result.success(all);
    }
  //根据名字查询部门
    @GetMapping("/{name}")
    public Result findByName(@PathVariable String name){
        Dept dept = deptService.findByName(name);
        return Result.success(dept);
    }
    @PostMapping
    public Result addDept(@RequestBody Dept dept){
        deptService.addDept(dept);
        return Result.success();
    }
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id){
        deptService.deleteById(id);
        return Result.success();
    }
    @PutMapping
    public Result updateById(@RequestBody Dept dept){
        deptService.updateDept(dept);
        return Result.success();
    }
    @GetMapping("/list")
    public Result listDepts(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "5") Integer pageSize) {

        PageBean pageData = deptService.getDeptByPage(page, pageSize);
        return Result.success(pageData);
    }
    @GetMapping("/count")
    public Result count() {
        Integer count = deptService.count();
        return Result.success(count);
    }
}
